package com.dsiab.portalService.service.impl;

import com.dsiab.portalService.entity.CommentDto;
import com.dsiab.portalService.entity.PagesDto;
import com.dsiab.portalService.mapper.PagesDao;
import com.dsiab.portalService.service.PagesSerice;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class PagesSericeImpl implements PagesSerice {

  @Autowired
  PagesDao pagesDao;

  @Autowired
  RedisTemplate redisTemplate;

  public List<PagesDto> getRecomList(String type) {

    List<PagesDto> list;

    if (type.equals("30")){
      list =  pagesDao.getRecomListBy30();
    } else if(type.equals("365")) {
      list = pagesDao.getRecomListBy365();
    } else {
      return pagesDao.getRecomList();
    }

    return list;
  }

  public Map<String,Object> getListByPage(String page){

    Map<String, Object> resultMap= new HashMap<String, Object>();
    int pageNo = Integer.parseInt(page);
    // pageHelper
    PageHelper.startPage(pageNo,10);
    List<PagesDto> list = pagesDao.getAll();
    PageInfo<PagesDto> pagesDtoPageInfo = new PageInfo<>(list);
    resultMap.put("result", pagesDtoPageInfo.getList());
    resultMap.put("total", pagesDtoPageInfo.getTotal());
    return resultMap;
  };

  public Object getListByCatePage(String cate,String page){
    Map<String, Object> resultMap = new HashMap<>();
    PageHelper.startPage(Integer.parseInt(page),10);
    List<PagesDto> list = pagesDao.getListByCate(Integer.parseInt(cate), Integer.parseInt(page));
    PageInfo<PagesDto> pagesDtoPageInfo = new PageInfo<>(list);
    resultMap.put("total", pagesDtoPageInfo.getTotal());
    resultMap.put("result", pagesDtoPageInfo.getList());
    return resultMap;
  };

  public Object getListByTagsPage(String tag,String page) {
    Map<String, Object> resultMap = new HashMap<>();
    PageHelper.startPage(Integer.parseInt(page),10);
    List<PagesDto> list = pagesDao.getListByTags(URLDecoder.decode(tag, StandardCharsets.UTF_8), Integer.parseInt(page));
    PageInfo<PagesDto> pagesDtoPageInfo = new PageInfo<>(list);
    resultMap.put("total", pagesDtoPageInfo.getTotal());
    resultMap.put("result", pagesDtoPageInfo.getList());
    return resultMap;
  };

  public Object getCateList(){
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDao.getCate());
    return resultMap;
  };

  public Object getTagsList(){
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDao.getTags());
    return resultMap;
  };

  public Map getDetailByPageId(String id){
    // 如果不想使用redis，可使用查数据库代码，然后注释redisTemplate部分65-69行代码
    // PagesDto pagesDto  = pagesSerice.getDetail(Integer.parseInt(id));
    PagesDto pagesDto = null;
    try {
      // 默认从缓存取，如果缓存没有，从数据库查询，同时存入redis
      pagesDto = (PagesDto) redisTemplate.opsForValue().get(id);
      if (pagesDto == null){
        if (id.indexOf("-") > 0){
          // 如果是uuid则通过Uuid查询
          pagesDto = pagesDao.getDetailByUUID(id);
        } else {
          // id为数字且超过5825不允许访问
          if (Integer.parseInt(id)< 5825){
            pagesDto = pagesDao.getDetail(Integer.parseInt(id));
          }
        }
        if (pagesDto!=null){
          //  添加版权信息
          pagesDto.setContent(pagesDto.getContent() + "<p style='opacity:0'><a href='https://www.dsiab.com' style='cursor:unset'>本文章由javascript技术分享原创和收集</a></p>");
          // 缓存数据到redis
          redisTemplate.opsForValue().set(id,pagesDto);
          // 设置redis数据2天有效
          redisTemplate.expire(id,2, TimeUnit.DAYS);
        }

      }
    } catch (Exception e) {
      //  如果未配置redis，则默认查数据库
      if (id.indexOf("-") > 0){
        // 如果是uuid则通过Uuid查询
        pagesDto = pagesDao.getDetailByUUID(id);
      } else {
        // id为数字且超过5825不允许访问
        if (Integer.parseInt(id)< 5825){
          pagesDto = pagesDao.getDetail(Integer.parseInt(id));
        }
      }
      pagesDto.setContent(pagesDto.getContent() + "<p style='opacity:0'><a href='https://www.dsiab.com' style='cursor:unset'>本文章由javascript技术分享原创和收集</a></p>");
    }
    if (pagesDto!=null){
      // 浏览量加1
      pagesDao.updateViews(pagesDto);
    }
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDto);
    return resultMap;
  };

  public List<PagesDto> getRecommendBySameTags(String id){
    // 兼容两种格式id
    String keywords = "";
    if (id.indexOf("-") > 0){
      // 如果是uuid则通过Uuid查询
      keywords = pagesDao.getDetailByUUID(id).getKeywords();
    } else {
      keywords = pagesDao.getDetail(Integer.parseInt(id)).getKeywords();
    }
    if (keywords.isEmpty()){
      return new ArrayList<>();
    }
    // 通过第一个关键字推荐文章
    if (keywords.indexOf(",") >0){
      String[] arr = keywords.split(",");
      keywords = arr[0];
    }
    return pagesDao.getRecommendBySameTags(keywords);
  }

  public Map getSiteInfo() {
    return pagesDao.getSiteInfo();
  }

  public Object getTopicList(){
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDao.getTopicList());
    return resultMap;
  };

  public Object getTopicDetailList(String topicId){
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDao.getTopicDetailList(Integer.parseInt(topicId)));
    return resultMap;
  };

  public void addComment(CommentDto CommentDto){
    pagesDao.addComment(CommentDto);
  }
  public List<CommentDto> getCommentByPostId(String postId){
    return pagesDao.getCommentByPostId(postId);
  }
}
